package nl.hanze.web.t41.http;

import java.io.File;

public class HTTPServer {

	public static final char dirSep = File.separatorChar;
	public static File webRoot;
	private HTTPHandlerImpl hhi;
	private HTTPListener hl;

	public HTTPServer(int port, File webRoot) throws Exception {
		hhi = new HTTPHandlerImpl();
		hl = new HTTPListener(port, hhi);
		hl.startUp(); // start the HTTPListener
	}

	public static void main(String[] args) {
		// default port
		int port = 8080;
		// default web root
		webRoot = new File("www");

		// get port from argument
		if (args.length > 0) {
			try {
				port = Integer.parseInt(args[0]);
			} catch (NumberFormatException e) {
				System.out.println("First argument needs to be an integer");
			}
		}
		// get web root from argument
		if (args.length > 1) {
			try {
				webRoot = new File(args[1]);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}

		// start HTTPServer
		try {
			new HTTPServer(port, webRoot);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
